{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "from langchain_core.prompts import PromptTemplate\n",
    "from langchain_core.prompts import FewShotPromptTemplate\n",
    "from dotenv import load_dotenv\n",
    "from langchain_community.chat_models import ChatTongyi"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "True"
      ]
     },
     "execution_count": 2,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "load_dotenv(dotenv_path='.qwen')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {},
   "outputs": [],
   "source": [
    "example_prompt = PromptTemplate.from_template(template= \"城市: {in}\\n 省份: {out}\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 构建给大模型的样例\n",
    "examples= [\n",
    "    {\"in\":\"厦门\", \"out\":\"福建\"},\n",
    "    {\"in\":\"福州\", \"out\":\"福建\"},\n",
    "    {\"in\":\"上海\", \"out\":\"上海\"},\n",
    "    {\"in\":\"北京\", \"out\":\"北京\"},\n",
    "    {\"in\":\"广州\", \"out\":\"广东\"},\n",
    "    {\"in\":\"深圳\", \"out\":\"广东\"},\n",
    "    {\"in\":\"成都\", \"out\":\"四川\"},\n",
    "    {\"in\":\"武汉\", \"out\":\"湖北\"},\n",
    "    {\"in\":\"西安\", \"out\":\"陕西\"}\n",
    "]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "metadata": {},
   "outputs": [],
   "source": [
    "prompt = FewShotPromptTemplate(example_prompt=example_prompt,examples=examples, \n",
    "                               prefix='请学习我给定的样例，并回答我提出的问题：\\n\"\"\"', \n",
    "                               suffix='\"\"\"\\n输入: {input}\\n输出：')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "metadata": {},
   "outputs": [],
   "source": [
    "result = prompt.invoke(input={\"input\":\"南平\"})"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 29,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "StringPromptValue(text='请学习我给定的样例，并回答我提出的问题：\\n\"\"\"\\n\\n城市: 厦门\\n 省份: 福建\\n\\n城市: 福州\\n 省份: 福建\\n\\n城市: 上海\\n 省份: 上海\\n\\n城市: 北京\\n 省份: 北京\\n\\n城市: 广州\\n 省份: 广东\\n\\n城市: 深圳\\n 省份: 广东\\n\\n城市: 成都\\n 省份: 四川\\n\\n城市: 武汉\\n 省份: 湖北\\n\\n城市: 西安\\n 省份: 陕西\\n\\n\"\"\"\\n城市: 大庆\\n省份：')"
      ]
     },
     "execution_count": 29,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "result"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "metadata": {},
   "outputs": [],
   "source": [
    "chat = ChatTongyi(model=\"qwen-max\", temperature=0.1, max_tokens=512)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "metadata": {},
   "outputs": [],
   "source": [
    "result = chat.invoke(result)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "AIMessage(content='省份: 福建\\n\\n南平市位于中国的福建省。根据您提供的样例格式，对于输入“南平”，对应的输出是其所属的省份，即福建。', additional_kwargs={}, response_metadata={'model_name': 'qwen-max', 'finish_reason': 'stop', 'request_id': '3e6e0110-bf5f-954f-8cf2-24b11c01f72c', 'token_usage': {'input_tokens': 169, 'output_tokens': 38, 'total_tokens': 207}}, id='run-40766dda-a44c-45bf-bfb1-2c1692565606-0')"
      ]
     },
     "execution_count": 19,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "result"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "metadata": {},
   "outputs": [],
   "source": [
    "chain = prompt | chat"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "content='省份: 江西\\n\\n根据您提供的样例格式，南昌位于中国的江西省。' additional_kwargs={} response_metadata={'model_name': 'qwen-max', 'finish_reason': 'stop', 'request_id': '60aac95c-80a7-92a8-9648-4196dd4682aa', 'token_usage': {'input_tokens': 169, 'output_tokens': 19, 'total_tokens': 188}} id='run-e3c018b1-181b-4311-8392-25651a359ba1-0'\n"
     ]
    }
   ],
   "source": [
    "print(chain.invoke(input={\"input\":\"南昌\"}))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "chat2money",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.10.16"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
